package com.inspur.dgov.bsp.application.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inspur.dgov.bsp.application.entity.PubResource;
import com.inspur.dgov.bsp.application.mapper.PubResourceMapper;
import com.inspur.dgov.bsp.application.service.PubResourceService;
import com.inspur.dgov.bsp.application.vo.ResourceVO;
import com.inspur.dgov.common.tool.constant.AppConstant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 应用资源信息 服务实现类
 *
 * @author chiyupei
 * @version 1.0
 * @since 1.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class PubResourceServiceImpl extends ServiceImpl<PubResourceMapper, PubResource> implements PubResourceService {

    /**
     * 前端菜单数据
     *
     * @param roleId
     * @return
     */
    @Override
    public List<ResourceVO> menus(String roleId, String appId) {
        List<PubResource> allMenus = this.allMenus(appId);
        List<PubResource> roleMenus = baseMapper.roleMenus(StrUtil.split(roleId, ','), appId);
        List<PubResource> routes = new LinkedList<>(roleMenus);
        roleMenus.forEach(roleMenu -> recursion(allMenus, routes, roleMenu));
        routes.sort(Comparator.comparing(PubResource::getSortOrder));
        List<ResourceVO> collect = routes.stream().map(resource -> BeanUtil.toBean(resource, ResourceVO.class)).collect(Collectors.toList());
        return collect;
    }

    /**
     * 资源分页数据
     * @param page
     * @param params
     * @return
     */
    @Override
    public IPage<ResourceVO> selectResourcePage(Page page, Map<String, Object> params) {
        return baseMapper.selectResourcePage(page, params);
    }

    /**
     * 迭代构建菜单树
     * @param allMenus
     * @param routes
     * @param roleMenu
     */
    private void recursion(List<PubResource> allMenus, List<PubResource> routes, PubResource roleMenu) {
        Optional<PubResource> menu = allMenus.stream().filter(x -> x.getId().equals(roleMenu.getParentId())).findFirst();
        if (menu.isPresent() && !routes.contains(menu.get())) {
            routes.add(menu.get());
            recursion(allMenus, routes, menu.get());
        }
    }

    /**
     * 获取应用全部菜单
     *
     * @param appId
     * @return
     */
    private List<PubResource> allMenus(String appId) {
        QueryWrapper<PubResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PubResource::getStatus, AppConstant.SYSTEM_ON)
                .eq(PubResource::getCategory, "1")
                .eq(PubResource::getAppId, appId).orderByAsc(PubResource::getSortOrder);
        List<PubResource> list = baseMapper.selectList(queryWrapper);
        return list;
    }
}
